package com.example.dee.controller;

import com.example.dee.config.ParamConfig;
import com.example.dee.pojo.CarData;
import com.example.dee.pojo.CarRecord;
import com.example.dee.pojo.Record;
import com.example.dee.pojo.User;
import com.example.dee.pojo.vo.CarDataUp;
import com.example.dee.pojo.vo.ResponseJSON;
import com.example.dee.service.SysService;
import com.example.dee.threadRun.UpDataRunnable;
import com.example.dee.util.RandomUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

import static com.example.dee.controller.CarDataUpController.doPost;

/**
 * @ClassName SysController
 * @创建人 李劲松
 * @创建时间 2022/7/20 10:19
 * @描述 A机构管理用户操作控制类
 */
@Controller
public class SysController {
    @Resource
    SysService sysService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User sysUser = (User)request.getSession().getAttribute("sysUser");
        if(sysUser==null){
            response.getWriter().write("<script>alert('请登录');</script>");
            return "login";
        }
        String func = request.getParameter("func");
        int begin=0;
        int showNum=50;
        if (ParamConfig.CAR_DATA_FUNC_SELECT.equals(func)){
            String state = request.getParameter("state");
            List<CarData> carDataList = sysService.selectCarDataByState(state,begin,showNum);
            request.setAttribute("carDataList",carDataList);
            return "carDataUp";
        }
        List<CarData> carDataList = sysService.selectCarDataByUp(begin,showNum);
        request.setAttribute("carDataList",carDataList);
        return "carDataUp";
    }
    @RequestMapping("/carData")
    public String carData(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User sysUser = (User)request.getSession().getAttribute("sysUser");
        if(sysUser==null){
            response.getWriter().write("<script>alert('请登录');</script>");
            return "login";
        }else{
            List<CarData> carDataList = new ArrayList<>();
            String func = request.getParameter("func");
            if (ParamConfig.CAR_DATA_FUNC_ADD.equals(func)){
                //新增操作
                String enTime = RandomUtil.getDateFormat(new Date());
                String enTollStationName = "济南";
                String exTime = RandomUtil.getDateFormat(new Date());
                String exTollStationName = "枣庄";
                String vehicleId = "鲁A78541_3";
                Integer vehicleType = 2;
                Double fee = 7.22;
                String serialDataNow = RandomUtil.getCarDataId();
                String id = "";
                for (int i = 0; i < 50 ; i++) {
                    int serial = sysService.selectSerial();
                    String serialData = sysService.selectSerialData();
                    if(serial==0||!serialDataNow.equals(serialData)){
                        id=serialDataNow+"000001";
                        serial=1;
                    }else {
                        id = serialDataNow+"000000".substring(0,6-(serial+"").length())+serial;
                    }
                    serial++;
                    sysService.updateSerial(serial,serialDataNow);
                    sysService.addCarData(id,enTime,enTollStationName,exTime,exTollStationName,vehicleId,vehicleType,fee);
                    int sysId = ((User) request.getSession().getAttribute("sysUser")).getId();
                    ParamConfig.fixedThreadPool.execute(new UpDataRunnable(id,sysId,sysService));
                    carDataList = sysService.selectCarDataAll();
                }
            }else if(ParamConfig.CAR_DATA_FUNC_DELETE.equals(func)){
                //删除操作
                String[] ids = request.getParameterValues("id");
                if(ids!=null){
                    for (String s:ids) {
                        CarData carData = sysService.selectCarData(s);
                        if(sysService.deleteCarData(s)>0){
                            sysService.addCarRecord(carData,ParamConfig.CAR_RECORD_DELETE,sysUser.getId());
                        }
                    }
                }
                carDataList = sysService.selectCarDataAll();

            }else if(ParamConfig.CAR_DATA_FUNC_UPDATE.equals(func)){
                //修改操作
                String id = request.getParameter("id");
                Double fee = Double.valueOf(request.getParameter("fee"));
                CarData carData = sysService.selectCarData(id);
                sysService.updateCarData(id,fee);
                sysService.addCarRecord(carData,ParamConfig.CAR_RECORD_UPDATE,sysUser.getId());
                carDataList = sysService.selectCarDataAll();
            }else if(ParamConfig.CAR_DATA_FUNC_SELECT.equals(func)){
                //查询操作
                String state = request.getParameter("state");
                int begin=0;
                int showNum=50;
                carDataList = sysService.selectCarDataByState(state,begin,showNum);
            }else{
                //界面显示
                 carDataList = sysService.selectCarDataAll();

            }
            request.setAttribute("carDataList",carDataList);
            return "carData";
        }
    }
    @RequestMapping("/command")
    public String command(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User sysUser = (User)request.getSession().getAttribute("sysUser");
        if(sysUser==null){
            response.getWriter().write("<script>alert('请登录');</script>");
            return "login";
        }else{
            String func = request.getParameter("func");
            if (ParamConfig.COMMAND_FUNC_UPDATE.equals(func)){
                String command = request.getParameter("command");
                if(sysService.updateCommand(command)>0){
                    response.getWriter().write("<script>alert('修改成功');</script>");
                }else{
                    response.getWriter().write("<script>alert('修改失败');</script>");
                }
            }
            String command = sysService.selectCommand();
            request.setAttribute("command",command);
            return "command";
        }
    }
    @RequestMapping("/upRecord")
    public String upRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User sysUser = (User)request.getSession().getAttribute("sysUser");
        if(sysUser==null){
            response.getWriter().write("<script>alert('请登录');</script>");
            return "login";
        }else{
            int begin=0;
            int showNum=ParamConfig.RECORD_SHOWNUM;
            int count = sysService.getCountByTable("t_record");
            if(request.getParameter("begin") != null){
                begin = Integer.parseInt(request.getParameter("begin"));
                if(begin<0){
                    begin=0;
                }else if(begin>=count){
                    begin=count-showNum;
                    if(begin<0){
                        begin=0;
                    }
                }
            }
            List<Record> recordList = sysService.selectRecord(RandomUtil.getDateYYYYMMDDFormat(new Date()),begin,showNum);
            request.setAttribute("beginUp",begin-showNum);
            request.setAttribute("beginDown",begin+showNum);
            request.setAttribute("recordList",recordList);
            return "upRecord";
        }
    }
    @RequestMapping("/delRecord")
    public String delRecord(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User sysUser = (User)request.getSession().getAttribute("sysUser");
        if(sysUser==null){
            response.getWriter().write("<script>alert('请登录');</script>");
            return "login";
        }else{
            int begin=0;
            int showNum=ParamConfig.CAR_RECORD_SHOWNUM;
            int count = sysService.getCountByTable("t_car_record");
            if(request.getParameter("begin") != null){
                begin = Integer.parseInt(request.getParameter("begin"));
                if(begin<0){
                    begin=0;
                }else if(begin>=count){
                    begin=count-showNum;
                    if(begin<0){
                        begin=0;
                    }
                }
            }
            List<CarRecord> carRecordList = sysService.selectCarRecord(RandomUtil.getDateYYYYMMDDFormat(new Date()),begin,showNum);
            request.setAttribute("beginUp",begin-showNum);
            request.setAttribute("beginDown",begin+showNum);
            request.setAttribute("carRecordList",carRecordList);
            return "delRecord";
        }
    }

}
